package com.demo.common.ratelimit.config;

import com.demo.common.ratelimit.aspect.ApiIdempotentAspect;
import com.demo.common.ratelimit.aspect.DistributeLockAspect;
import com.demo.common.ratelimit.condition.LimiterCondition;
import com.demo.common.ratelimit.interceptor.UserApiInterceptor;
import com.demo.common.ratelimit.interceptor.UserWebInterceptor;
import com.demo.common.ratelimit.lock.RedisLock;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;

/**
 * bean初始化
 *
 * @author DayDayUp
 * @datetime 2023-09-02 13:51
 **/
public class BeanInitConfig {
    @Bean
    @Conditional(LimiterCondition.class)
    @ConditionalOnProperty(prefix = RateLimiterProperties.PREFIX, name = "enabled", havingValue = "true",matchIfMissing = true)
    public UserApiInterceptor userApiInterceptor() {
        return new UserApiInterceptor();
    }

    @Bean
    @Conditional(LimiterCondition.class)
    @ConditionalOnProperty(prefix = RateLimiterProperties.PREFIX, name = "enabled", havingValue = "true",matchIfMissing = true)
    public UserWebInterceptor userWebInterceptor() {
        return new UserWebInterceptor();
    }

    @Bean
    public ApiIdempotentAspect apiIdempotentAspect() {
        return new ApiIdempotentAspect();
    }

    @Bean
    public DistributeLockAspect distributeLockAspect() {
        return new DistributeLockAspect();
    }

    @Bean
    public RedisLock redisLock() {
        return new RedisLock();
    }
}
